<html>
<head>

<title>Groovy Goodness: Date and Time Durations and the TimeCategory</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Date and Time Durations and the TimeCategory</h3>

<div class="post">
<p>Groovy has some elegant ways to work with date and time values. One of them is the support of durations. We can define a duration to denote a certain time amount, like 7 days, 2 hours and 50 minutes. We can use these durations to add or subtract them from date and time objects.</p>
<p>The <code>TimeCategory</code> provides an even Groovier way to work with durations. We can use constructs like 7.days + 12.minutes to create a duration. When we read this code it is just like reading English text. Here is some sample code:</p>
<pre class="brush:groovy">
import groovy.time.*
import org.codehaus.groovy.runtime.TimeCategory

// Define period of 2 years, 3 months, 15 days, 0 hours, 23 minutes, 2 seconds and 0 milliseconds.
def period = new DatumDependentDuration(2, 3, 15, 0, 23, 2, 0)
assert '2 years, 3 months, 15 days, 23 minutes, 2.000 seconds' == period.toString()
def year2000 = new Date(100, 0, 0)  // Jan 1, 2000
assert 'Mon Apr 15 00:23:02 UTC 2002' == (period + year2000).toString()

// Define time period of 5 hours, 54 minutes and 30 milliseconds.
def time = new TimeDuration(5, 54, 0, 30)
assert '5 hours, 54 minutes, 0.030 seconds' == time.toString()

use (TimeCategory) {
    assert period.toString() == (2.years + 3.months + 15.days + 0.hour + 23.minutes + 2.seconds).toString()
    assert time.toString() == (5.hours + 54.minutes + 30.milliseconds).toString()

    // We can use period.from.now syntax.    
    def d1 = 1.week - 1.day
    def d2 = new Date() + 6.days
    assert d2.format('yyyy-MM-dd') == d1.from.now.toString()
    
    // We can use period.ago syntax.
    def d3 = 3.days.ago
    def d4 = new Date() - 3
    assert d4.format('yyyy-MM-dd') == d3.toString()
}
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>